﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.OleDb;

/// <summary>
/// Descripción breve de Player
/// </summary>
[AjaxPro.AjaxNamespace("Player")] 
public class Player
{
    private int _Pk_Player;    
    private string _Player_Name;    
    private string _Player_Nick;
    private int _Player_Number;   
    private string _Player_PhotoPath;
    private int _Fk_PlayerZone;
    private string _Player_Zone_Flag;
    private Int64  _PlayerScore;
    private string _PlayerNickNumber;

    public string PlayerNickNumber
    {
        get { return _PlayerNickNumber; }
        set { _PlayerNickNumber = value; }
    }

    public Int64 PlayerScore
    {
        get { return _PlayerScore; }
        set { _PlayerScore = value; }
    }

    public int Pk_Player
    {
        get { return _Pk_Player; }
        set { _Pk_Player = value; }
    }
    public string Player_Name
    {
        get { return _Player_Name; }
        set { _Player_Name = value; }
    }
    public string Player_Nick
    {
        get { return _Player_Nick; }
        set { _Player_Nick = value; }
    }
    public int Player_Number
    {
        get { return _Player_Number; }
        set { _Player_Number = value; }
    }
    public string Player_PhotoPath
    {
        get { return _Player_PhotoPath; }
        set { _Player_PhotoPath = value; }
    }
    public int Fk_PlayerZone
    {
        get { return _Fk_PlayerZone; }
        set { _Fk_PlayerZone = value; }
    }
    public string Player_Zone_Flag
    {
        get { return _Player_Zone_Flag; }
        set { _Player_Zone_Flag = value; }
    }

	public Player()
	{

    }

    public Player(int p_Pk_Player)
    {
        try
        {
            DataTable oDataTable = DataBase.dt("SELECT Pk_Player,Player_Name,Player_Nick,Player_PhotoPath,Fk_PlayerZone,Player_Number FROM Players WHERE Pk_Player=" + p_Pk_Player.ToString() );

            if (oDataTable.Rows.Count > 0)
            {
               this._Pk_Player = Int32.Parse(oDataTable.Rows[0]["Pk_Player"].ToString());
               this._Player_Name = oDataTable.Rows[0]["Player_Name"].ToString();
               this._Player_Nick = oDataTable.Rows[0]["Player_Nick"].ToString();
               this._Player_Number = Int32.Parse(oDataTable.Rows[0]["Player_Number"].ToString());
               this.Fk_PlayerZone = Int32.Parse(oDataTable.Rows[0]["Fk_PlayerZone"].ToString());
                
        
            }
            
          
           
        }
        catch (Exception Ex)
        {
            throw Ex;
        }
    }

    public static Player GetPlayerByNumber(int p_Player_Number)
    {
        try
        {
            DataTable oDataTable = DataBase.dt("SELECT Pk_Player,Player_Name,Player_Nick,Player_PhotoPath,Fk_PlayerZone,Player_Number FROM Players WHERE Player_Number=" + p_Player_Number.ToString());
            Player oPlayer = new Player() ;
            if (oDataTable.Rows.Count > 0)
            {
                oPlayer = new Player(Int32.Parse(oDataTable.Rows[0]["Pk_Player"].ToString()),
                                     oDataTable.Rows[0]["Player_Name"].ToString(),
                                     oDataTable.Rows[0]["Player_Nick"].ToString(),
                                     Int32.Parse(oDataTable.Rows[0]["Player_Number"].ToString()),
                                     "",
                                     Int32.Parse(oDataTable.Rows[0]["Fk_PlayerZone"].ToString()));
 

            }
            return oPlayer;
    

        }
        catch (Exception Ex)
        {
            throw Ex;
        }
    }


    public Player(int p_Player_Number, Int64 p_Total_Player_Score)
    {
        try
        {
            this._Player_Number = p_Player_Number;
            this._PlayerScore = p_Total_Player_Score; 

        }
        catch (Exception Ex)
        {
            throw Ex;
        }
    }

    public Player(int p_Pk_Player, string p_Player_Name, string p_Player_Nick, int p_Player_Number, string p_Player_PhotoPath, int p_Fk_PlayerZone)
    {
        this._Pk_Player = p_Pk_Player;
        this._Player_Name = p_Player_Name;
        this._Player_Nick = p_Player_Nick;
        this._Player_PhotoPath = p_Player_PhotoPath;
        this._Fk_PlayerZone = p_Fk_PlayerZone;
        this._Player_Number = p_Player_Number;
        this._PlayerNickNumber = p_Player_Number.ToString() + " (" + p_Player_Nick + ")";
 
    }

    public Player(string p_Player_Name, string p_Player_Nick, string p_Player_PhotoPath, int p_Player_Number, int p_Fk_PlayerZone)
    {        
        this._Player_Name = p_Player_Name;
        this._Player_Nick = p_Player_Nick;
        this._Player_PhotoPath = p_Player_PhotoPath;
        this._Fk_PlayerZone = p_Fk_PlayerZone;
        this._Player_Number = p_Player_Number;
        this._PlayerNickNumber = p_Player_Number.ToString() + "(" + p_Player_Nick + ")";
    }
    
    public static List<Player> LoadPlayersFlagZones(List<Player> p_PlayerList) { 
        //TODO: Cargamos las banderas asociadas.
        return null;
    }

    public static List<Player> GetPlayersByTournament(int p_Pk_Tournament) {
        try
        {
            DataTable oDataTable = DataBase.dt("SELECT Pk_Player,Player_Name,Player_Nick,Player_PhotoPath,Fk_PlayerZone,Player_Number FROM Players P INNER JOIN PlayerTournament PT ON P.Pk_Player=PT.Fk_Player WHERE PT.Fk_Tournament=" + p_Pk_Tournament.ToString() + " ORDER BY Player_Number");

            List<Player> ReturnList = new List<Player>();

            foreach (DataRow iRow in oDataTable.Rows)
            {
                Player oPlayer = new Player(Int32.Parse(iRow["Pk_Player"].ToString()),iRow["Player_Name"].ToString(), iRow["Player_Nick"].ToString(),int.Parse(iRow["Player_Number"].ToString()), iRow["Player_PhotoPath"].ToString(),int.Parse(iRow["Fk_PlayerZone"].ToString()));
                ReturnList.Add(oPlayer);
            }
            return ReturnList;
        }
        catch (Exception Ex)
        {
            throw Ex;
        }

    }

    public static List<Player> GetAllPlayers()
    {
        try
        {
            DataTable oDataTable = DataBase.dt("SELECT Pk_Player,Player_Name,Player_Nick,Player_PhotoPath,Fk_PlayerZone,Player_Number FROM Players P ORDER BY Player_Number");

            List<Player> ReturnList = new List<Player>();

            foreach (DataRow iRow in oDataTable.Rows)
            {
                Player oPlayer = new Player(Int32.Parse(iRow["Pk_Player"].ToString()), iRow["Player_Name"].ToString(), iRow["Player_Nick"].ToString(), int.Parse(iRow["Player_Number"].ToString()), iRow["Player_PhotoPath"].ToString(), int.Parse(iRow["Fk_PlayerZone"].ToString()));
                ReturnList.Add(oPlayer);
            }
            return ReturnList;
        }
        catch (Exception Ex)
        {
            throw Ex;
        }

    }

    public static int CreatePlayer(Player p_Player,int[] p_Tournaments){
     OleDbConnection objConnection = new OleDbConnection();
        try
        {
            //TODO controlar si hay un playerNumber duplicado

            objConnection = DataBase.GetConnection();
            OleDbCommand objCommand = new OleDbCommand("INSERT INTO Players (Player_Name,Player_Nick,Player_PhotoPath,Fk_PlayerZone,Player_Number) VALUES ('" +
                                                        p_Player.Player_Name.ToString() + "','" + p_Player.Player_Nick.ToString() + "','" + p_Player.Player_PhotoPath.ToString() 
                                                        + "'," + p_Player.Fk_PlayerZone.ToString() + ","+p_Player.Player_Number +")", objConnection);
            objConnection.Open();
            objCommand.ExecuteNonQuery();

            OleDbCommand cmdGetIdentity = new OleDbCommand();
            cmdGetIdentity.CommandText = "SELECT @@IDENTITY";
            cmdGetIdentity.Connection = objConnection;

            int Identity = Int32.Parse(cmdGetIdentity.ExecuteScalar().ToString());
            
            //Insertamos la relación con el/los torneos
            foreach (int oItem in p_Tournaments) {

                objCommand = new OleDbCommand("INSERT INTO PlayerTournament (Fk_Player,Fk_Tournament) VALUES (" + 
                                               Identity.ToString() + "," + oItem.ToString() + ")", objConnection);
                objCommand.ExecuteNonQuery();

            }  
           
            return Identity;


        }
        catch (Exception Ex)
        {
            throw Ex;
        }
        finally
        {
            objConnection.Close();
        }
    }

    public static Boolean DeletePlayer(int p_Pk_Player)
    {
        OleDbConnection objConnection = new OleDbConnection();
        try
        {
            //TODO controlar si hay un playerNumber duplicado
            objConnection = DataBase.GetConnection();
            OleDbCommand objCommand = new OleDbCommand("DELETE * FROM PlayerTournament WHERE Fk_Player=" + p_Pk_Player.ToString(), objConnection);
            objConnection.Open();
            objCommand.ExecuteNonQuery();
           
            objCommand = new OleDbCommand("DELETE * FROM Players WHERE Pk_Player="+p_Pk_Player.ToString(), objConnection);
            objCommand.ExecuteNonQuery();

            return true;


        }
        catch (Exception Ex)
        {
            return false;
        }
        finally
        {
            objConnection.Close();
        }
    }


}
